package ru.javadevelop.vegetarian.fragments;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import ru.javadevelop.vegetarian.R;
import ru.javadevelop.vegetarian.activities.HomeActivity;
import ru.javadevelop.vegetarian.controls.HeaderTitles;


public class HeaderFragment extends Fragment {
    private Context _context;
    private HeaderTitles headerTitles;
    private TextView slash;
    private TextView page_title;
    private TextView logo;
    private  View view;
    TextView back_breadcrumb;

    //******************************************************************************************************************
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        _context = getActivity();
        view =  inflater.inflate(R.layout.fragment_header,
                container, false);
        findControls();
        fillControls();
        setListeners();
        return view;
    }
    //******************************************************************************************************************
    @Override
    public void onCreate( Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }
    //******************************************************************************************************************
    public void setHeaderTitles(HeaderTitles headerTitles)
    {
        this.headerTitles = headerTitles;

        fillControls();
    }
    //******************************************************************************************************************
    private void findControls()
    {
        back_breadcrumb = (TextView)view.findViewById(R.id.back_breadcrumb);
        slash = (TextView)view.findViewById(R.id.slash);
        page_title = (TextView)view.findViewById(R.id.page_title);
        logo = (TextView)view.findViewById(R.id.logo);
    }
    //******************************************************************************************************************
    private void fillControls()
    {
        if (headerTitles != null)
        {
            if (headerTitles.backBreadcrumbIntent!=null)
            {
                back_breadcrumb.setOnClickListener(new OnBackBreadcrumbClick(headerTitles.backBreadcrumbIntent));
                back_breadcrumb.setText(headerTitles.backBreadcrumbText);
            }
            else
            {
                back_breadcrumb.setVisibility(View.INVISIBLE);
                slash.setVisibility(View.INVISIBLE);
            }
            if ( (headerTitles.headerText!=null)&&(!headerTitles.headerText.equals("")) )
            {
                page_title.setText(headerTitles.headerText);
            }
        }
    }
    //******************************************************************************************************************
    private void setListeners()
    {
        logo.setOnClickListener(new OnLogoClick());
    }
    //******************************************************************************************************************
    private class OnBackBreadcrumbClick implements View.OnClickListener
    {
        private  Intent intent;
        public OnBackBreadcrumbClick(Intent intent)
        {
            this.intent = intent;
        }

        @Override
        public void onClick(View v) {
            intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(intent);
        }
    }
    //******************************************************************************************************************
    private class OnLogoClick implements View.OnClickListener
    {

        @Override
        public void onClick(View v) {
            Intent i = new Intent(_context, HomeActivity.class);
            startActivity(i);
        }
    }
    //******************************************************************************************************************
    //******************************************************************************************************************
    //******************************************************************************************************************
}
